/**
 * 
 */
package jmai.evaluator;

/**
 * @author Jean-Pierre
 *
 */
public class Functions {
	
	
	/**
	 * call() provides interface to all functions
	 * @param fn the name of the function to be called
	 * @param in the input string
	 * @param a the first parameter
	 * @param b the second parameter
	 * @return the result in String format
	 */
	public static String call(String fn, String in, String a, String b, Boolean debug) {
		
		String result = in;		
		
		if(fn.equals("insert")) {
			if(a.equals("EOL"))
				result = Functions.insert(in, in.length(), b.charAt(0));
			else
				result = Functions.insert(in, Integer.parseInt(a), b.charAt(0));
		} else if(fn.equals("replace")) {
			result = Functions.replace(in, a.charAt(0), b.charAt(0));
		} else if(fn.equals("substring")) {
			int c,d;
			if(a.equals("EOL"))
				c = in.length();
			else
				c = Integer.parseInt(a);
			
			if(b.equals("EOL"))
				d = in.length();
			else
				d = Integer.parseInt(b);
			result = Functions.substring(in, c, d);
		} else if(fn.equals("print") && !debug) {
			result = Functions.print(in);
		} else if(fn.equals("remove")) {
			result = Functions.remove(in, a.charAt(0));
		} else {
			return null;
		}
		
		if(debug) {
			System.out.println("input : " + in);
			System.out.println(fn + " : " + result);
		}
		
		return result;
	}

	/**
	 * replace() replaces all a with b in the input string
	 * @param src input string
	 * @param a character to replace
	 * @param b replacement character
	 * @return the result in String format
	 */
	private static String replace(String src, char a, char b) {
		char[] dst = src.toCharArray();
		
		for(int i=0; i<dst.length; i++) {
			if(dst[i] == a) {
				dst[i] = b;
			}
		}
		
		return (new String(dst));
	}
	
	/**
	 * insert() inserts a character at position x
	 * @param src input string
	 * @param x position to insert in front of
	 * @param a character to be inserted
	 * @return the result in String format
	 */
	private static String insert(String src, int x, char a) {
		char[] tmp = src.toCharArray();
		char[] dst = new char[tmp.length+1];
		
		if(x > (tmp.length - 1) || x == -1) {
			x = tmp.length;
		} else if(x < 0) {
			x = 0;
		}
		
		for(int i=0; i<x; i++) {
			dst[i] = tmp[i];
		}
		
		dst[x] = a;
		
		for(int i=(x+1); i<dst.length; i++) {
			dst[i] = tmp[i-1];
		}
		
		return (new String(dst));
	}
	
//	private static String insert(String src, String eol, char a) {
//		if(eol.equals("EOL")) {
//			return insert(src, src.length(), a);
//		} else {
//			return src;
//		}
//	}
	
	/**
	 * remove() removes all occurences of a
	 * @param src input string
	 * @param a character to remove
	 * @return the result in String format
	 */
	private static String remove(String src, char a) {
		char[] tmp = src.toCharArray();
		
		int l = 0;
		
		for(int i=0; i<tmp.length; i++) {
			if(tmp[i] != a)
				l++;
		}
		
		char[] dst = new char[l];
		
		int j = 0;
		
		for(int i=0; i<tmp.length; i++) {
			if(tmp[i] != a) {
				dst[j] = tmp[i];
				j++;
			}
		}
		
		return (new String(dst));
	}
	
	/**
	 * print() prints the input string to console
	 * @param src input string
	 */
	private static String print(String src) {
		System.out.println(src);
		return src;
	}
	
	/**
	 * substring() returns the substring of the input string from x to y (not inclusive)
	 * @param src input string
	 * @param x start position
	 * @param y end position
	 * @return the result in String format
	 */
	private static String substring(String src, int x, int y) {
		char[] tmp = src.toCharArray();

		if(x < 0) {
			x = 0;
		} else if(x > tmp.length-1) {
			x = tmp.length;
		}
		
		if(y < 0) {
			y = 0;
		} else if(y > tmp.length-1) {
			y = tmp.length;
		}
		
		if(x >= y) {
			return "";
		}
		
		char[] dst = new char[y-x];
		
		int j = 0;
		for(int i=x; i<y; i++) {
			dst[j] = tmp[i];
			j++;
		}
		
		return (new String(dst));
	}
	
//	private static String substring(String src, int x, String eol) {
//		if(eol.equals("EOL")) {
//			return substring(src, x, src.length());
//		} else {
//			return src;
//		}
//	}
}